<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
	<meta charset="UTF-8">
	<title>采购到货录入</title>
	<meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0, shrink-to-fit=no' name='viewport' />
	<link rel="stylesheet" href="assets/css/bootstrap.min.css">
	<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i">
	<link rel="stylesheet" href="assets/css/ready.css">
</head>
<body>
	<div class="wrapper">
		<div class="main-header">
			<div class="logo-header">
				<a href="enterProduct.html" class="logo">
					艾尔之光仓库管理
				</a>
				<button class="navbar-toggler sidenav-toggler ml-auto" type="button" data-toggle="collapse" data-target="collapse" aria-controls="sidebar" aria-expanded="false" aria-label="Toggle navigation">
					<span class="navbar-toggler-icon"></span>
				</button>
				<button class="topbar-toggler more"><i class="la la-ellipsis-v"></i></button>
			</div>
		</div>
		<div class="sidebar">
			<div class="scrollbar-inner sidebar-wrapper">
				<ul class="nav">
					<li class="nav-item">
						<a href="enterProduct.html">
							<i class="la la-th"></i>
							<p>商品录入</p>
						</a>
					</li>
					<li class="nav-item">
						<a href="enterInventory.html">
							<i class="la la-th"></i>
							<p>库存录入</p>
						</a>
					</li>
					<li class="nav-item">
						<a href="enterPurchase.html">
							<i class="la la-th"></i>
							<p>采购预录</p>
						</a>
					</li>
					<li class="nav-item active">
						<a href="enterProduct.html">
							<i class="la la-th"></i>
							<p>采购到货录入</p>
						</a>
					</li>
					<li class="nav-item">
						<a href="export.html">
							<i class="la la-th"></i>
							<p>导出Execl</p>
						</a>
					</li>
					<li class="nav-item">
						<a href="updateSalesperson.html">
							<i class="la la-th"></i>
							<p>修改业务员姓名</p>
						</a>
					</li>
				</ul>
			</div>
		</div>
		<div class="main-panel">
			<div class="content">
				<div class="container-fluid">
					<div class="card">
						<div class="card-header">
							<div class="card-title">采购到货录入</div>
						</div>
						<div class="card-body">

							<div class="form-group form-inline">
								<label for="contractCode" class="col-md-3 col-form-label">合同编号</label>
								<div class="col-md-9 p-0">
									<input type="text" list="contractCodeList" class="form-control input-full" id="contractCode" placeholder="请输入合同编号">
									<datalist id="contractCodeList">
									</datalist>
								</div>
							</div>
							<div class="form-group form-inline">
								<label for="modelName" class="col-md-3 col-form-label">型号</label>
								<div class="col-md-9 p-0">
									<input type="text" list="modeNameList" class="form-control input-full" id="modelName" placeholder="请输入商品型号">
									<datalist id="modeNameList">
									</datalist>
								</div>
							</div>
							<div class="form-group form-inline">
								<label for="arrivedQty" class="col-md-3 col-form-label">到货数量</label>
								<div class="col-md-9 p-0">
									<input type="text" class="form-control input-full" id="arrivedQty" placeholder="请输入到货数量">
								</div>
							</div>
							<div class="form-group form-inline">
								<label for="arrivedDate" class="col-md-3 col-form-label">到货时间</label>
								<div class="col-md-9 p-0">
									<input type="text" class="form-control input-full" id="arrivedDate" placeholder="请输入到货时间，（为空则取当前时间，输入格式为YYYY-mm-dd）">
								</div>
							</div>
						</div>
						<div class="card-action">
							<button id="enterGoodsArrived" class="btn btn-success">录入</button>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>

</body>
<script src="assets/js/core/jquery.3.2.1.min.js"></script>
<script src="assets/js/plugin/jquery-ui-1.12.1.custom/jquery-ui.min.js"></script>
<script src="assets/js/core/popper.min.js"></script>
<script src="assets/js/core/bootstrap.min.js"></script>
<script src="assets/js/plugin/chartist/chartist.min.js"></script>
<script src="assets/js/plugin/chartist/plugin/chartist-plugin-tooltip.min.js"></script>
<script src="assets/js/plugin/bootstrap-notify/bootstrap-notify.min.js"></script>
<script src="assets/js/plugin/bootstrap-toggle/bootstrap-toggle.min.js"></script>
<script src="assets/js/plugin/jquery-mapael/jquery.mapael.min.js"></script>
<script src="assets/js/plugin/jquery-mapael/maps/world_countries.min.js"></script>
<script src="assets/js/plugin/chart-circle/circles.min.js"></script>
<script src="assets/js/plugin/jquery-scrollbar/jquery.scrollbar.min.js"></script>
<script src="assets/js/ready.min.js"></script>
<script>



	$("#modelName").focus(function (){
		var modeNameList = document.getElementById('modeNameList');
		while (modeNameList.hasChildNodes()) {
			modeNameList.removeChild(modeNameList.lastChild);
		}
		var contractCode = $("#contractCode").val();
		var json = {
			"contractCode":contractCode
		};
		json = JSON.stringify(json)//转化为json字符串
		//获取所有的型号
		$.ajax({
			url: "/purchase_get_model_name_by_contract_code",
			headers:{
				'Content-Type': 'application/json' // 确保这里的 Content-Type 与服务器端期望的格式一致
			},
			type: 'post',
			data: json,
			async: false,   //如果不加，无法实现数据传值
			dataType: 'json',
			success: function (datajson) {
				var result = datajson;
				if (result.code == "0000"){
					var data = result.data
					$(data).each(function (key) {  //循环读取后台传来的Json数据
						modelNameNode(data[key], data[key]);   //将value和text添加到下拉框中
					});
				}else {
					alert(result.message)
				}
			},
			error: function () {
				alert("出现错误");
			}
		});
		function modelNameNode(value, text) {
			$("#modeNameList").append("<option value='" + value + "'>" + text + "</option>");
		};
	});

	//获取所有的合同编号
	$.ajax({
		url: "/purchase_all_contract_code",
		type: 'post',
		async: false,   //如果不加，无法实现数据传值
		dataType: 'json',
		success: function (datajson) {
			var result = datajson;
			if (result.code == "0000"){
				var data = result.data
				$(data).each(function (key) {  //循环读取后台传来的Json数据
					contractCodeNode(data[key], data[key]);   //将value和text添加到下拉框中
				});
			}else {
				alert(result.message)
			}
		},
		error: function () {
			alert("出现错误");
		}
	});
	function contractCodeNode(value, text) {
		$("#contractCodeList").append("<option value='" + value + "'>" + text + "</option>");
	};


	//录入功能
	$("#enterGoodsArrived").click(function (){
		var contractCode = $("#contractCode").val();
		var modelName = $("#modelName").val();
		var arrivedQty = parseInt($("#arrivedQty").val());
		var arrivedDate = $("#arrivedDate").val();
		var json = {
			"contractCode":contractCode,
			"modelName":modelName,
			"arrivedQty":arrivedQty,
			"arrivedDate":arrivedDate
		};
		if (!contractCode){
			alert("请输入合同编号");
			throw new Error("合同编号输入异常");
		}
		if (!modelName){
			alert("请输入型号");
			throw new Error("型号输入异常");
		}
		if (!arrivedQty){
			alert("请输入合法的数量");
			throw new Error("数量输入异常");
		}
		json = JSON.stringify(json)//转化为json字符串
		$.ajax({
			url: "/enter_goods_arrived",
			headers:{
				'Content-Type': 'application/json' // 确保这里的 Content-Type 与服务器端期望的格式一致
			},
			type: 'post',
			data: json,
			async: false,   //如果不加，无法实现数据传值
			dataType: 'json',
			success: function (datajson) {
				console.log(datajson)
				alert(datajson["message"])
			},
			error: function (){
				alert("录入失败")
			}
		});
	});


</script>
</html>